$(function() {

	mui.init({
		pullRefresh: {
			container: refreshContainer, //待刷新区域标识，querySelector能定位的css选择器均可，比如：id、.class等
			up: {
				height: 30, //可选.默认50.触发上拉加载拖动距离
				auto: false, //可选,默认false.自动上拉加载一次
				contentrefresh: "正在加载...", //可选，正在加载状态时，上拉加载控件上显示的标题内容
				contentnomore: '没有更多数据了', //可选，请求完毕若没有更多数据时显示的提醒内容；
				callback: puful //必选，刷新函数，根据具体业务来编写，比如通过ajax从服务器获取新数据；
			}
		}
	});
	var record = 0; //每次返回的数据的长度
	//请求JSON
	var userid = localStorage.getItem('userid');
	var token = localStorage.getItem('token');
	//ajax请求处理数据
	mui.showLoading("正在加载..", ".warn-list");
	var url = domain + "/dtwbgl/wbcqlist.h5";
	var req = {
		currtime: getNowFormatDate(),
		page: "1",
		pagenum: Dpage,
		userid: userid,
		token: token,
	}
	//上拉加载刷新
	function puful() {
		req.page = parseInt(req.page) + 1; //页码
		Request();
		console.log(record);
		if (record < req.pagenum) {
			this.endPullupToRefresh(true);
		} else {
			setTimeout(function() {
				mui('#refreshContainer').pullRefresh().enablePullupToRefresh();
				mui('#refreshContainer').pullRefresh().endPullupToRefresh();
			}, 1500)
		}
	}
	var kinds = {
		"1": {
			"name": "维保",
			"class": ""
		},
		"2": {
			"name": "紧急",
			"class": "wei",
		}
	};
	//内容区加载
	mui.ready(function() {
		//加载框
		mui.showLoading("正在加载..", ".warn-list");
		Request();
	});
	// 重新获取数据或触发刷新
	window.addEventListener("pageRefresh", function() {
		Request();
	});

	function Request() {
		$.ajax(url, {
			//data: JSON.stringify(req),
			data: req,
			dataType: 'json', //服务器返回json格式数据
			type: 'post', //HTTP请求类型
			timeout: 30000, //超时时间
			headers: {
				'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
			},
			async: true, //true异步加载
			success: function(data) {
				if (data.code == 2001) {
					console.log(data);
					var Nolist = data.data.rows;

					var comref = Nolist.sort(compare('starttime')); //排序
					var html = '';
					// console.log(Nolist);
					record = Nolist.length;

					console.log('返回天数' + record);
					Nolist.forEach(function(e, k) {

						// console.log(e);
						html +=
							`<li >
								<a data-status=${e.actorstatu} data-caseid=${e.caseid} data-actionid=${e.actionid} data-kind=${e.kind}>
									<!--物业-->
									<div class="warn-bianma">
										<h2>物业名称：${e.wyname}</h2>
										<div class="warn-bianma-state">
											<p class="active">										
												<i>已超期</i><span>${e.cqday} 天</span>
											</p>
										</div>
									</div>
									<div class="warn-zhucema">								
										<h3>工单号：${e.caseid}</h2>
									</div>
									<!--内部编码-->
									<div class="warn-zhucema">								
										<h3>内部编码：${e.nbbm}</h2>
									</div>
									<!--电梯注册码-->
									<div class="warn-zhucema">
										<h3>电梯注册码：${e.dtcode}</h2>
									</div>		
									<!--横线半圆样式-->
									<div class="warn-hx">
										<i></i><i></i>
									</div>
									<!--时间-->
									<div class="warn-time mui-row">
										<div class="mui-col-xs-6">
											<p>${e.endtime}日截止</p>
										</div>					
									</div>
								</a>
							</li>`;
					});
					$(".warn-list").append(html)
					//隐藏加载框				
					mui.hideLoading(function() { //隐藏后的回调函数
					});
					mui('.warn-list').on('tap', 'li a', function() {
						var actorstatu = $(this).data("status")
						var caseid = $(this).data("caseid");
						var actionid = $(this).data("actionid");
						var kind = $(this).data("kind")
						var index = $('.warn-list li').index(this);
						var _this = this;
						//var lats=$('body').attr("data-lat");					
						//var lags=$('body').attr("data-lng");	
						// 2019-01-08
						var lats = localStorage.getItem('wbapplat');
						var lags = localStorage.getItem('wbapplng');

						var data = {
							kind: kind,
							actionid: actionid,
							time: getNowFormatDate(),
							bz2: '',
							userid: userid,
							long: lags,
							lat: lats,
							token: token,
							caseid: caseid,
							type: "1"
						};
						if (actorstatu == "10") {
							$('.up_inner').css('position', 'fixed').show(200);
							$(".close_a").click(function() {
								$(".up_inner").hide();
							})
							$('.Agree').off('click').on('click', function() {
								data.type = "1";
								// console.log(eleIndex);
								mui.showLoading("正在加载..", ".warn-list");
								taskAjax(data, index);
								$('.up_inner').hide();
							});
							localStorage.setItem('caseid', caseid);

						} else if (actorstatu == "20") {
							localStorage.setItem('caseid', caseid);
							mui.openWindow({
								url: 'over-list-detail.html',
								id: 'over-list-detail.html',
							});
						} else {
							mui.openWindow({
								url: '../../404.html',
								id: '../../404.html'
							})
						}
					})
				} else if (data.code == 2002) {
					mui.alert(data.msg);
					mui.openWindow({
						url: '../../login.html',
						id: '../../login.html'
					})
				} else {
					mui.alert(data.msg);
					mui.openWindow({
						url: '../../404.html',
						id: '../../404.html'
					})
				}
			},
			error: function(xhr, type, errorThrown) { //异常处理； console.log(type);
				mui.alert(type);
				mui.openWindow({
					url: '../../login.html',
					id: 'login.html'
				})
			}
		});

	};
	var taskAjax = function(data, index) {
		$.ajax(domain + "/dtwbgl/wbrecv.h5", {
			data: JSON.stringify(data),
			dataType: 'json', //服务器返回json格式数据
			type: 'post', //HTTP请求类型
			timeout: 10000, //超时时间设置为10秒；
			headers: {
				'Content-Type': 'application/json'
			},
			success: function(res) {
				if (res.code == 3001) { // success
					// 根据data.cms判断是拒绝还是接收
					// console.log(data);
					if (data.type == "1") {
						mui.alert("接收工单成功");
						// mui('#slider').slider().gotoItem(1);						
						mui.openWindow({
							url: 'over-list-detail.html',
							id: 'over-list-detail.html',
						});
					} else {
						mui.alert("接收工单失败");
						mui.hideLoading(function() { //隐藏后的回调函数  
						});
					}

					// $("#scroll1").find('li:eq("' + index + '")').remove(); // remove
					mui.hideLoading(function() { //隐藏后的回调函数  
					});
				} else {
					mui.toast(res.msg);
					return false;
				}
			},
			error: function(xhr, type, errorThrown) {
				console.log(type);
				mui.alert(type);
			}
		});
	}
	//排序
	function compare(property) {
		return function(a, b) {
			var value1 = a[property];
			var value2 = b[property];
			return Date.parse(value1) - Date.parse(value2);
		}
	}
})
